package com.xiaorun.portal.controller.admin;


import com.github.pagehelper.PageInfo;
import com.xiaorun.portal.constant.WebConst;
import com.xiaorun.portal.controller.BaseController;
import com.xiaorun.portal.dto.LogActions;
import com.xiaorun.portal.dto.Types;
import com.xiaorun.portal.exception.TipException;
import com.xiaorun.portal.modal.Bo.RestResponseBo;
import com.xiaorun.portal.modal.Vo.Carousel;
import com.xiaorun.portal.modal.Vo.ContentVo;
import com.xiaorun.portal.modal.Vo.MetaVo;
import com.xiaorun.portal.modal.Vo.UserVo;
import com.xiaorun.portal.service.ICarouselService;
import com.xiaorun.portal.service.IContentService;
import com.xiaorun.portal.service.ILogService;
import com.xiaorun.portal.service.IMetaService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 13 on 2018/11/21.
 */
@Controller
@RequestMapping("/admin/carousel")
@Slf4j
public class CarouselController extends BaseController {
    @Autowired
    private ICarouselService carouselService;

    @RequestMapping(value = "/list")
    public String list(@RequestBody(required = false) Map<String, Object> params, HttpServletRequest request) {
        return this.list(1, 10, params, request);
    }

    @RequestMapping(value = "/list/{pageNum}/{pageSize}")
    public String list(@PathVariable int pageNum, @PathVariable int pageSize, @RequestBody(required = false) Map<String, Object> params, HttpServletRequest request) {
        params = params != null ? params : new HashMap<>(16);
        if(StringUtils.isBlank((String)params.get("type"))){
            params.put("type", "_CAROUSELS");
        }

        PageInfo<Carousel> carousels = carouselService.list(pageNum, pageSize, params);
        request.setAttribute("carousels", carousels);
        return "admin/carousel_list";
    }

    @RequestMapping(value = "/search")
    public String search(String type, HttpServletRequest request) {
        Map<String, Object>  params = new HashMap<>(1);
        params.put("type", type);
        return this.list(1, 10, params, request);
    }



    @GetMapping(value = "/add")
    public String add(HttpServletRequest request) {
        return "admin/carousel_edit";
    }

    @GetMapping(value = "/edit/{id}")
    public String editPage(@PathVariable Integer id, HttpServletRequest request) {
        Carousel carousel = carouselService.get(id);
        request.setAttribute("carousel", carousel);
        return "admin/carousel_edit";
    }

    @PostMapping(value = "/save")
    @ResponseBody
    @Transactional(rollbackFor = TipException.class)
    public RestResponseBo save(Carousel carousel, HttpServletRequest request) {
        try {
            carouselService.save(carousel);
        } catch (Exception e) {
            String msg = "编辑失败";
            if (e instanceof TipException) {
                msg = e.getMessage();
            }
            log.error(msg, e);
            return RestResponseBo.fail(msg);
        }

        return RestResponseBo.ok();
    }

    @RequestMapping(value = "/del/{id}")
    @ResponseBody
    @Transactional(rollbackFor = TipException.class)
    public RestResponseBo delete(@PathVariable int id, HttpServletRequest request) {
        try {
            carouselService.remove(id);
        } catch (Exception e) {
            String msg = "删除失败";
            if (e instanceof TipException) {
                msg = e.getMessage();
            } else {
                log.error(msg, e);
            }
            return RestResponseBo.fail(msg);
        }
        return RestResponseBo.ok();
    }
}
